package com.magenta.maxoptra.gis;

import com.magenta.maxoptra.gis.util.Apis;

import javax.persistence.Column;
import java.io.Serializable;
/**
 * Key for persistent state of geocoding requests.
 * @author ivictus
 *
 */
public class ForwardGoecodingPK implements Serializable {
	private static final long serialVersionUID = -1276160817109318583L;

	static final int MAX_QUERY_LEN = 255;

    public Long cid;
    @SQL.Charset
	@Column ( precision = MAX_QUERY_LEN )
	public String query;
    public short index;

    public ForwardGoecodingPK() { }

    public ForwardGoecodingPK(Long cid, String query) {
    	this.cid = cid;
    	this.query = query;
    }

    public ForwardGoecodingPK(String providerName, String versionApi, String cidDiscriminator, String query) {
    	this(Apis.cid(providerName, versionApi, cidDiscriminator), query);
    }

    private int hash(Object o) {
        return o != null ? o.hashCode() : 0;
    }

    @Override
    public int hashCode() {
        int result = (hash(cid) << 5 | hash(cid) >> 32 - 5);
        result ^= (hash(query) << 17 | hash(query) >> 32 - 17);
        result ^= (hash(index) << 19 | hash(index) >> 32 - 19);
        return result;
    }

    private boolean eq(Object obj1, Object obj2) {
        return obj1 == obj2 || (obj1 != null && obj1.equals(obj2));
    }

    private boolean vectorEq(ForwardGoecodingPK pk) {
        return eq(cid, pk.cid) && eq(query, pk.query) && eq(index, pk.index);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof ForwardGoecodingPK && vectorEq((ForwardGoecodingPK) obj);
    }

    public int getMaxQueryLength() {
    	return MAX_QUERY_LEN;
    }

	/**
	 * As array representation.
	 */
	public Object[] toArray() {
		return new Object[] {cid, query, index};
	}
}
